package com.yang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.common.Meta;

import com.yang.common.MetaTotal;
import com.yang.common.Result;

import com.yang.pojo.*;
import com.yang.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

import java.util.Map;

@Controller
public class SearchController {

    @Autowired
    private SearchService service;


    /**
     * 根据关键字来进行搜索，并进行分页查询
     * @param keyword
     * @return
     */
    @ResponseBody
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")

    public Result searchByMsg(@PathVariable String keyword,
                              @PathVariable int pageNo,
                              @PathVariable int pageSize) throws IOException {
        MetaTotal metas = service.searchAllHighLight(keyword,pageNo,pageSize);

        Page<Meta> page = new Page<>(pageNo,pageSize);
        page.setRecords(metas.getList());
        page.setTotal(metas.getTotal());
        return new Result().code(200).message("查询成功").data("list",metas.getList()).data("total",metas.getTotal());
    }

    @Autowired
    private DefectService defectService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private RiskControllerService riskControllerService;
    @Autowired
    private SecurityService securityService;
    @Autowired
    private WorkticketService workticketService;

    /**
     * 根据传过来的id以及数据库表名来进行查看详情
     * @param id
     * @param map
     * @return
     */

//    @RequestMapping("/details/{id}")
//    public String look_details(@PathVariable("id") String id, Map<String,Object> map){
////        System.out.println("过来了" + e);
//        String[] str = id.split("001_");
//
//        if (str[0].equals("defect")){
//            Defect defect = defectService.getById(str[1]);
//            map.put(FLAG,defect);
//        } else if (str[0].equals("device")) {
//            Device device = deviceService.getById(str[1]);
//            map.put(FLAG,device);
//        } else if (str[0].equals("riskcontroller")) {
//            Riskcontroller riskcontroller = riskControllerService.getById(str[1]);
//            map.put(FLAG,riskcontroller);
//        }else if (str[0].equals("security")) {
//            Security security = securityService.getById(str[1]);
//            map.put(FLAG,security);
//        }else if (str[0].equals("workticket")) {
//            Workticket workticket = workticketService.getById(str[1]);
//            map.put(FLAG,workticket);
//        }
//        System.out.println("map的size " + map.size());
//        System.out.println("map的内容 " + map.toString());
//        return "details";
//    }

    @RequestMapping("/details/{id}")
    @ResponseBody
    public Result look_details2(@PathVariable("id") String id, Map<String,Object> map){
        String[] str = id.split("001_");
        if (str[0].equals("defect")){
            Defect defect = defectService.getById(str[1]);
            return new Result().code(200).message("详情结果").data("details",defect);
        } else if (str[0].equals("device")) {

            Device device = deviceService.getById(str[1]);
            return new Result().code(200).message("详情结果").data("details",device);
        } else if (str[0].equals("riskcontroller")) {
            Riskcontroller riskcontroller = riskControllerService.getById(str[1]);
            return new Result().code(200).message("详情结果").data("details",riskcontroller);
        }else if (str[0].equals("security")) {
            Security security = securityService.getById(str[1]);
            return new Result().code(200).message("详情结果").data("details",security);
        }else if (str[0].equals("workticket")) {
            Workticket workticket = workticketService.getById(str[1]);
            return new Result().code(200).message("详情结果").data("details",workticket);
        }
        return new Result().code(500).message("查询失败");
    }
}
